package com.liaozl.sca.common.dto;

import com.liaozl.sca.common.enums.ResultCodeEnum;
import lombok.Data;

@Data
public class Result<T> extends BaseDTO {

    private Integer code;

    private String message;

    private T data;

    public boolean isSuccess() {
        return ResultCodeEnum.SUCCESS.getCode().equals(code);
    }

    public static Result success() {
        Result result = new Result();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    public static Result success(Object data) {
        Result result = success();
        result.setData(data);
        return result;
    }

    public static Result failed() {
        Result result = new Result();
        result.setCode(ResultCodeEnum.FAILED.getCode());
        result.setMessage(ResultCodeEnum.FAILED.getMessage());
        return result;
    }

    public static Result failed(ResultCodeEnum codeEnum) {
        Result result = new Result();
        result.setCode(codeEnum.getCode());
        result.setMessage(codeEnum.getMessage());
        return result;
    }

    public static Result failed(String errorMessage) {
        Result result = new Result();
        result.setCode(ResultCodeEnum.FAILED.getCode());
        result.setMessage(errorMessage);
        return result;
    }

    public static Result failed(Integer errorCode, String errorMessage) {
        if (errorCode == null || errorCode < 0) {
            return failed();
        }

        Result result = new Result();
        result.setCode(errorCode);
        result.setMessage(errorMessage);

        return result;
    }

    public static Result failed(Object data) {
        Result result = failed();
        result.setData(data);
        return result;
    }

    public static Result notLogin() {
        Result result = new Result();
        result.setCode(ResultCodeEnum.NOT_LOGIN.getCode());
        result.setMessage(ResultCodeEnum.NOT_LOGIN.getMessage());
        return result;
    }

    public static Result paramError(String paramName) {
        Result result = new Result();
        result.setCode(ResultCodeEnum.PARAM_ERROR.getCode());
        result.setMessage(String.format(ResultCodeEnum.PARAM_ERROR.getMessage(), paramName));
        return result;
    }

    public static Result systemError() {
        Result result = new Result();
        result.setCode(ResultCodeEnum.SYSTEM_ERROR.getCode());
        result.setMessage(ResultCodeEnum.SYSTEM_ERROR.getMessage());
        return result;
    }

}
